{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8c497753",
   "metadata": {},
   "source": [
    "* 2022年10月11日\n",
    "* 记录人：陈铮铮"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "291cd60c",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    " >1. 代码的注释\n",
    " >2. 用户输入\n",
    " >3. 关于格式化文本信息"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42b621ce",
   "metadata": {},
   "source": [
    "## 代码的注释\n",
    "> 1. #,一般写在代码块的最上边，或者每一行代码的最好，用于代码思路的记录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb618b5b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Author:Zhichao"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c00744d6",
   "metadata": {},
   "source": [
    " >2.“注释内容”，一般是注释代码块"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab408650",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c113c178",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "import randon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7f5363a8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "username:czz\n",
      "密码:12345\n",
      "czz\n",
      "12345\n"
     ]
    }
   ],
   "source": [
    "# 登陆系统\n",
    "username = input(\"username:\")\n",
    "password = input(\"密码:\")\n",
    "\n",
    "print(username)\n",
    "print(password)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "97775c92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'czz'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "username"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6ff26379",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "username:czz\n",
      "密码:12345\n",
      "欢迎czz登录！\n"
     ]
    }
   ],
   "source": [
    "# 登陆系统 完善\n",
    "# 假设数据库有用户信息：账号：czz，密码，12345\n",
    "user1 = \"czz\"\n",
    "password1 = \"12345\"\n",
    "\n",
    "username = input(\"username:\")\n",
    "password = input(\"密码:\")\n",
    "\n",
    "# 情景1：信息正确，登陆成功\n",
    "if user1 == username and password1 == password:\n",
    "    print(\"欢迎\"+user1+\"登录！\")\n",
    "\n",
    "# 情景2：账号不存在或密码不正确\n",
    "else:\n",
    "    print(\"账号不存在或密码不正确\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d039a489",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "username:czz\n",
      "密码:123456\n",
      "账号不存在或密码不正确\n"
     ]
    }
   ],
   "source": [
    "# 登陆系统 完善\n",
    "# 假设数据库有用户信息：账号：czz，密码，12345\n",
    "user1 = \"czz\"\n",
    "password1 = \"12345\"\n",
    "\n",
    "username = input(\"username:\")\n",
    "password = input(\"密码:\")\n",
    "\n",
    "# 情景1：信息正确，登陆成功\n",
    "if user1 == username and password1 == password:\n",
    "    print(\"欢迎\"+user1+\"登录！\")\n",
    "\n",
    "# 情景2：账号不存在或密码不正确\n",
    "else:\n",
    "    print(\"账号不存在或密码不正确\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d50853c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 登陆系统 迭代1：增加了判断功能\n",
    "# 假设数据库有用户信息：账号：czz，密码，12345\n",
    "user1 = \"czz\"\n",
    "password1 = \"12345\"\n",
    "\n",
    "\n",
    "username = input(\"username:\")\n",
    "password = input(\"密码:\")\n",
    "\n",
    "# 情景1：信息正确，登陆成功\n",
    "if user1 == username and password1 == password:\n",
    "    print(\"欢迎\"+user1+\"登录！\")\n",
    "\n",
    "# 情景2：账号不存在或密码不正确\n",
    "else:\n",
    "    print(\"账号不存在或密码不正确\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "809f14be",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (1744759720.py, line 17)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [11]\u001b[1;36m\u001b[0m\n\u001b[1;33m    else:\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# 登陆系统 迭代2：增加循环功能\n",
    "# 假设数据库有用户信息：账号：czz，密码，12345\n",
    "user1 = \"czz\"\n",
    "password1 = \"12345\"\n",
    "\n",
    "username = input(\"username:\")\n",
    "password = input(\"密码:\")\n",
    "\n",
    "# 情景1：信息正确，登陆成功\n",
    "if user1 == username and password1 == password:\n",
    "    print(\"欢迎\"+user1+\"登录！\")\n",
    "\n",
    "# 情景2：账号不存在或密码不正确\n",
    "else:\n",
    "    print(\"账号不存在或密码不正确\")\n",
    "# 情景3：输错直到正确为止\n",
    "else:\n",
    "    range(\"username \n",
    "password = input(\"密码:\")\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f45404d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 登陆系统 迭代2\n",
    "# 假设数据库有用户信息：账号：czz，密码，12345\n",
    "user1 = \"czz\"\n",
    "password1 = \"12345\"\n",
    "\n",
    "for i in range(3,0,-1):\n",
    "username = input(\"username:\")\n",
    "password = input(\"密码:\")\n",
    "\n",
    "# 情景1：信息正确，登陆成功\n",
    "if user1 == username and password1 == password:\n",
    "    print(\"欢迎\"+user1+\"登录！\")\n",
    "    break # 终止循环\n",
    "\n",
    "# 情景2：账号不存在或密码不正确\n",
    "else:\n",
    "    print(\"账号不存在或密码不正确\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69966663",
   "metadata": {},
   "source": [
    "## 格式化输出\n",
    "> 1. 字符串拼接\n",
    "> 2. 占位符\n",
    "> 3. format()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8b6d894e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：czz\n",
      "年龄：18\n",
      "工作：学生\n",
      "收入：无\n",
      "地区：广州\n",
      "-----INFO OF czz------\n",
      "年龄：18\n",
      "工作：学生\n",
      "收入：无\n",
      "地区：广州\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法一：字符串拼接\n",
    "name = input(\"姓名：\")\n",
    "age = input(\"年龄：\")\n",
    "job = input(\"工作：\")\n",
    "salary = input(\"收入：\")\n",
    "reg = input(\"地区：\")\n",
    "\n",
    "info = '''-----INFO OF '''+ name +'''------'''+'''\n",
    "年龄：'''+age+'''\n",
    "工作：'''+job+'''\n",
    "收入：'''+salary+'''\n",
    "地区：'''+reg\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "12f4a688",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "年龄：18\n",
      "18 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# input 作为用户输入，其都以字符串形式返回\n",
    "age = input(\"年龄：\")\n",
    "print(age,type(age))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "edbf20fd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：小A\n",
      "年龄：20\n",
      "工作：程序员\n",
      "收入：1w+\n",
      "地区：上海\n",
      "-----INFO OF 小A------\n",
      "姓名：小A\n",
      "年龄：20\n",
      "工作：程序员\n",
      "收入：1w+\n",
      "地区：上海\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法二：占位符 %s str, 依次有序输入\n",
    "name = input(\"姓名：\")\n",
    "age = int(input(\"年龄：\")) # 强制将 str 转换成 int\n",
    "job = input(\"工作：\")\n",
    "salary = input(\"收入：\")\n",
    "reg = input(\"地区：\")\n",
    "\n",
    "info = '''-----INFO OF %s------\n",
    "姓名：%s\n",
    "年龄：%d\n",
    "工作：%s\n",
    "收入：%s\n",
    "地区：%s'''%(name,name,age,job,salary,reg)\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "67b8d944",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：小B\n",
      "年龄：14\n",
      "工作：学生\n",
      "收入：无\n",
      "地区：未知\n",
      "-----INFO OF 小B------\n",
      "姓名：小B\n",
      "年龄：14\n",
      "工作：学生\n",
      "收入：无\n",
      "地区：未知\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法三：format() 格式化输出函数，参数传递，不需要考虑位置\n",
    "name = input(\"姓名：\")\n",
    "age = int(input(\"年龄：\")) # 强制将 str 转换成 int\n",
    "job = input(\"工作：\")\n",
    "salary = input(\"收入：\")\n",
    "reg = input(\"地区：\")\n",
    "\n",
    "info = '''-----INFO OF {姓名}------\n",
    "姓名：{姓名}\n",
    "年龄：{年龄}\n",
    "工作：{工作}\n",
    "收入：{收入}\n",
    "地区：{地区}'''.format(年龄 = age,姓名 = name,工作 = job,收入 = salary, 地区 = reg)\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2f82396",
   "metadata": {},
   "source": [
    "## 课后练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bd885c75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额:1000\n",
      "------- goods detail -------\n",
      "编号0，商品名称：面膜,商品价格: 200 元\n",
      "编号1，商品名称：运动裤,商品价格: 300 元\n",
      "编号2，商品名称：粉底液,商品价格: 300 元\n",
      "编号3，商品名称：挂烫机,商品价格: 200 元\n",
      "\n",
      "------goods detail-------\n"
     ]
    }
   ],
   "source": [
    "# 准备工作\n",
    "# 1、商品信息\n",
    "goods_list = [[\"面膜\",200],[\"运动裤\",300],[\"粉底液\",300],[\"挂烫机\",200]]\n",
    "#### 嵌套\n",
    "# 2、用户购物信息\n",
    "user_shopping_list = [\"\"]\n",
    "# 用户输入（余额）:\n",
    "alipay = input(\"请输入余额:\")\n",
    "info = '''------- goods detail -------\n",
    "编号0，商品名称：%s,商品价格: %s 元\n",
    "编号1，商品名称：%s,商品价格: %s 元\n",
    "编号2，商品名称：%s,商品价格: %s 元\n",
    "编号3，商品名称：%s,商品价格: %s 元\n",
    "'''%(goods_list[0][0],goods_list[0][1],\n",
    "     goods_list[1][0],goods_list[1][1],\n",
    "     goods_list[2][0],goods_list[2][1],\n",
    "     goods_list[3][0],goods_list[3][1])\n",
    "print(info)\n",
    "while True:\n",
    "    print(\"------goods detail-------\")\n",
    "    break\n",
    "    # for循环优势，节省重复代码的书写\n",
    "    for i in range(4):  # i是0，1，2，3\n",
    "        print(\"编号%s, 商品名称%s：%s元\" %(i,goods_list[i][0],goods_list[i][1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0396204c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额:200\n",
      "------- goods detail -------\n",
      "编号0，商品名称：面膜,商品价格: 200 元\n",
      "编号1，商品名称：运动裤,商品价格: 300 元\n",
      "编号2，商品名称：粉底液,商品价格: 300 元\n",
      "编号3，商品名称：挂烫机,商品价格: 200 元\n",
      "\n",
      "------goods detail-------\n"
     ]
    }
   ],
   "source": [
    "# 准备工作\n",
    "# 1、商品信息\n",
    "goods_list = [[\"面膜\",200],[\"运动裤\",300],[\"粉底液\",300],[\"挂烫机\",200]]\n",
    "#### 嵌套\n",
    "# 2、用户购物信息\n",
    "user_shopping_list = [\"\"]\n",
    "# 用户输入（余额）:\n",
    "alipay = input(\"请输入余额:\")\n",
    "info = '''------- goods detail -------\n",
    "编号0，商品名称：%s,商品价格: %s 元\n",
    "编号1，商品名称：%s,商品价格: %s 元\n",
    "编号2，商品名称：%s,商品价格: %s 元\n",
    "编号3，商品名称：%s,商品价格: %s 元\n",
    "'''%(goods_list[0][0],goods_list[0][1],\n",
    "     goods_list[1][0],goods_list[1][1],\n",
    "     goods_list[2][0],goods_list[2][1],\n",
    "     goods_list[3][0],goods_list[3][1])\n",
    "print(info)\n",
    "while True:\n",
    "    print(\"------goods detail-------\")\n",
    "    break\n",
    "    # for循环优势，节省重复代码的书写\n",
    "    for i in range(4):  # i是0，1，2，3\n",
    "        print(\"编号%s, 商品名称%s：%s元\" %(i,goods_list[i][0],goods_list[i][1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fa5fe09b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：200\n",
      "输入错误，您还有2次机会\n",
      "请输入余额：300\n",
      "输入错误，您还有1次机会\n",
      "请输入余额：1000\n",
      "您输入的余额正确！\n",
      "请输入购买商品的编号：3\n",
      "您的商品购买成功，已添加到购物车，您的余额还有800\n",
      "购买清单\n",
      "编号3，商品名称挂烫机：200元\n",
      "over!\n"
     ]
    }
   ],
   "source": [
    "# 2. 用户购物信息\n",
    "user_shopping_list = []\n",
    "# 用户输入（余额）：  # ------知识点： 打印与用户输入------\n",
    "\n",
    "# 加入循环？\n",
    "count = 3\n",
    "while True:\n",
    "    money_balance = 1000\n",
    "    Alipay = int(input(\"请输入余额：\"))\n",
    "    count -=1\n",
    "\n",
    "    if Alipay == money_balance:\n",
    "        print(\"您输入的余额正确！\")\n",
    "        choice = int(input(\"请输入购买商品的编号：\"))\n",
    "        ## 1. 输入编号存在，判断余额是否够\n",
    "        if choice < len(goods_list):\n",
    "            ### 1. 余额够，用户余额 - 商品价格\n",
    "            if goods_list[choice][1] <= Alipay:\n",
    "                Alipay = Alipay - goods_list[choice][1]\n",
    "                print(\"您的商品购买成功，已添加到购物车，您的余额还有%s\" % (Alipay))\n",
    "                print(\"购买清单\\n编号%s，商品名称%s：%s元\"%(choice,goods_list[choice][0],goods_list[choice][1]))\n",
    "                break\n",
    "            ### 2. 余额不够，“您的余额不足”\n",
    "            else:\n",
    "                print(\"您的余额不足，无法购买\")\n",
    "        ## 2. 输入的编号不存在，“请输入正确的余额”\n",
    "        # 2. 输入错误，“请输入正确的余额”\n",
    "        else:\n",
    "            print(\"您输入的编号不存在，请重新输入\")\n",
    "            choice_2 = int(input(\"重新输入购买商品的编号：\"))\n",
    "            if choice_2 < len(goods_list):\n",
    "                ### 1. 余额够，用户余额 - 商品价格\n",
    "                if goods_list[choice_2][1] <= Alipay:\n",
    "                    Alipay = Alipay - goods_list[choice_2][1]\n",
    "                    print(\"您的商品购买成功，已添加到购物车，您的余额还有%s\" % (Alipay))\n",
    "                    print(\"购买清单\\n编号%s，商品名称%s：%s元\" % (choice_2, goods_list[choice_2][0], goods_list[choice_2][1]))\n",
    "                    break\n",
    "\n",
    "                ### 2. 余额不够，“您的余额不足”\n",
    "                else:\n",
    "                    print(\"您的余额不足，无法购买\")\n",
    "                break\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "    else:\n",
    "        if count == 0:\n",
    "            print(\"您的账号无法正常使用\")\n",
    "            break\n",
    "        else:\n",
    "            print(\"输入错误，您还有%d次机会\" % count)\n",
    "print(\"over!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ed5a58df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：1000\n",
      "您输入的余额正确！\n",
      "请输入购买商品的编号：2\n",
      "您的商品购买成功，已添加到购物车，您的余额还有700\n",
      "购买清单\n",
      "编号2，商品名称粉底液：300元\n",
      "over!\n"
     ]
    }
   ],
   "source": [
    "# 2. 用户购物信息\n",
    "user_shopping_list = []\n",
    "# 用户输入（余额）：  # ------知识点： 打印与用户输入------\n",
    "\n",
    "# 加入循环？\n",
    "count = 3\n",
    "while True:\n",
    "    money_balance = 1000\n",
    "    Alipay = int(input(\"请输入余额：\"))\n",
    "    count -=1\n",
    "\n",
    "    if Alipay == money_balance:\n",
    "        print(\"您输入的余额正确！\")\n",
    "        choice = int(input(\"请输入购买商品的编号：\"))\n",
    "        ## 1. 输入编号存在，判断余额是否够\n",
    "        if choice < len(goods_list):\n",
    "            ### 1. 余额够，用户余额 - 商品价格\n",
    "            if goods_list[choice][1] <= Alipay:\n",
    "                Alipay = Alipay - goods_list[choice][1]\n",
    "                print(\"您的商品购买成功，已添加到购物车，您的余额还有%s\" % (Alipay))\n",
    "                print(\"购买清单\\n编号%s，商品名称%s：%s元\"%(choice,goods_list[choice][0],goods_list[choice][1]))\n",
    "                break\n",
    "            ### 2. 余额不够，“您的余额不足”\n",
    "            else:\n",
    "                print(\"您的余额不足，无法购买\")\n",
    "        ## 2. 输入的编号不存在，“请输入正确的余额”\n",
    "        # 2. 输入错误，“请输入正确的余额”\n",
    "        else:\n",
    "            print(\"您输入的编号不存在，请重新输入\")\n",
    "            choice_2 = int(input(\"重新输入购买商品的编号：\"))\n",
    "            if choice_2 < len(goods_list):\n",
    "                ### 1. 余额够，用户余额 - 商品价格\n",
    "                if goods_list[choice_2][1] <= Alipay:\n",
    "                    Alipay = Alipay - goods_list[choice_2][1]\n",
    "                    print(\"您的商品购买成功，已添加到购物车，您的余额还有%s\" % (Alipay))\n",
    "                    print(\"购买清单\\n编号%s，商品名称%s：%s元\" % (choice_2, goods_list[choice_2][0], goods_list[choice_2][1]))\n",
    "                    break\n",
    "\n",
    "                ### 2. 余额不够，“您的余额不足”\n",
    "                else:\n",
    "                    print(\"您的余额不足，无法购买\")\n",
    "                break\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "    else:\n",
    "        if count == 0:\n",
    "            print(\"您的账号无法正常使用\")\n",
    "            break\n",
    "        else:\n",
    "            print(\"输入错误，您还有%d次机会\" % count)\n",
    "print(\"over!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a561f5db",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
